package com.zhtools.wx.bean;

import com.zhtools.wx.WXPayRequestParams;
import com.zhtools.wx.exception.WXPayException;
import com.zhtools.wx.utils.WXPayUtil;
import com.zhtools.wx.utils.WXUtil;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanghao
 * 下载对账单请求参数
 */
public class DownLoadBillRquest implements WXPayRequestParams, Serializable {
    private static final long serialVersionUID = 1554470783034981737L;
    private static final String EMPTY_STRING = "";
    private static final String TRANSFORM_EXCEPTION = "转换下载微信支付对账单接口请求报文异常";

    private String billDate;
    private String billType;

    public String getBillDate() {
        return billDate;
    }

    public void setBillDate(String billDate) {
        this.billDate = billDate;
    }

    public String getBillType() {
        return billType;
    }

    public void setBillType(String billType) {
        this.billType = billType;
    }

    @Override
    public String toString() {
        return "DownLoadBillRquest{" +
                "billDate='" + billDate + '\'' +
                ", billType='" + billType + '\'' +
                '}';
    }

    @Override
    public String toXml(String appId, String mchId, String signKey) throws WXPayException {
        return toXml(appId,mchId,null,signKey);
    }

    @Override
    public String toXml(String appId, String mchId, String notifyUrl, String signKey) throws WXPayException {
        String nonce_str = WXUtil.generateNonceStr();

        Map<String, String> params = new HashMap<>();
        params.put("appid", appId);
        params.put("mch_id", mchId);
        params.put("nonce_str", nonce_str);
        params.put("bill_date", this.billDate);
        params.put("bill_type", this.billType);

        String requestXml = EMPTY_STRING;
        try {
            requestXml = WXPayUtil.generateSignedXml(params, signKey);
        } catch (Exception e) {
            throw new WXPayException(TRANSFORM_EXCEPTION, e);
        }
        return requestXml;
    }
}
